﻿using AdvancedTool.Standard;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ParkSaleFeatureDemo
{
    public class AsyncTask : AsyncTasking<Object>
    {
        public delegate Object BackGroundAction();
        /// <summary>
        /// 预处理方法
        /// </summary>
        /// <param name="self"></param>
        public delegate void PreAction();
        /// <summary>
        /// 结果处理方法
        /// </summary>
        /// <param name="result"></param>
        /// <param name="self"></param>
        public delegate void ResultAction(Object result);

        BackGroundAction main;
        PreAction pre;
        ResultAction result;

        protected override void PreInit()
        {
            if (pre != null)
                pre();
        }

        protected override Object BackgroundTask()
        {
            return main();
        }
        protected override void PostResult(Object result, AsyncTasking<Object> self)
        {

            this.result(result);
        }

        public void Run(BackGroundAction backGround, ResultAction result, PreAction pre = null)
        {
            if (backGround == null)
            {
                throw new Exception("后台任务不可为空");
            }
            if (result == null)
            {
                throw new Exception("结果处理任务不能为空");
            }
            this.main = backGround;
            this.result = result;
            this.pre = pre;
            this.InvokeAsync();
        }
    }
}
